package com.apache.bid.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.bid.common.Constant;
import com.apache.bid.entity.BidInfoJingjia;
import com.apache.bid.manager.BidInfoJingjiaManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.tools.DateUtils;

public class BidInfoJingjiaManagerImpl implements BidInfoJingjiaManager {

	protected IDao bidInfoJingjiaDao;
	protected final String entityName = "com.apache.bid.entity.BidInfoJingjia";

	public void setBidInfoJingjiaDao(IDao bidInfoJingjiaDao) {
		this.bidInfoJingjiaDao = bidInfoJingjiaDao;
	}

	public String saveInfo(ParamsVo<BidInfoJingjia> vo) throws BusinessException {
		BidInfoJingjia bidInfoJingjia = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="bidInfoJingjia_"+infoId;
		if (Validator.isNull(bidInfoJingjia.getBidId())) {
			bidInfoJingjia.setBidId(infoId);
		}
		if (Constant.getInstance().ifWriteDatabase()) {
			bidInfoJingjia.setBidTime(DateUtils.Now.fmt_yyyyMMdd_HHmmssSSS());
			MethodParam param = new MethodParam("BidInfoJingjia", cacheKey, "", entityName);
			param.setVaule(bidInfoJingjia);
			if (bidInfoJingjiaDao.insert(param)) {
				return bidInfoJingjia.getBidId();
			}
		} else {
			///////////////需要处理缓存结果//////////////////
			return bidInfoJingjia.getBidId();
		}
		return "";
	}

	public boolean editInfo(ParamsVo<BidInfoJingjia> vo) throws BusinessException {
		BidInfoJingjia bidInfoJingjia = vo.getObj();
		if (Validator.isNotNull(bidInfoJingjia.getBidId())) {
			String cacheKey = "";
			//String cacheKey="bidInfoJingjia_"+bidInfoJingjia.getBidId();
			if (Constant.getInstance().ifWriteDatabase()) {
				MethodParam param = new MethodParam("BidInfoJingjia", cacheKey, "", entityName);
				param.setVaule(bidInfoJingjia);
				return bidInfoJingjiaDao.edit(param);
			} else {
				///////////////需要处理缓存结果//////////////////
				return true;
			}
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<BidInfoJingjia> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="bidInfoJingjia_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		if (Constant.getInstance().ifWriteDatabase()) {
			MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
			if ("false".equals(mark)) {//逻辑删除
				param.setInfoId(infoId);
				BidInfoJingjia info = (BidInfoJingjia) bidInfoJingjiaDao.selectById(param);
				if (Validator.isEmpty(info)) {
					return false;
				}
				param.setVaule(info);//此处需要先将状态值赋值为删除状态
				param.setKey("BidInfoJingjia");
				return bidInfoJingjiaDao.edit(param);
			} else {
				String key = Validator.getDefaultStr(vo.getMethodKey(), "ById");
				param.setKey(key);
				if ("ById".equals(key)) {
					param.setParams("bidId", infoId);
				} else {
					param.setParams("itemId", infoId);
				}
				param.setDelete(true);
				return bidInfoJingjiaDao.delete(param);
			}
		} else {
			///////////////需要处理缓存结果//////////////////
			if ("false".equals(mark)) {//逻辑删除
			} else {
			}
			return true;
		}
	}

	public Object getInfoById(ParamsVo<BidInfoJingjia> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="bidInfoJingjia_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return bidInfoJingjiaDao.selectById(param);
	}

	public Object execute(ParamsVo<BidInfoJingjia> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<BidInfoJingjia> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = bidInfoJingjiaDao.pageSelect(param);
		return page;
	}

	public List<BidInfoJingjia> getList(ParamsVo<BidInfoJingjia> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return bidInfoJingjiaDao.select(param);
	}

	public long countInfo(ParamsVo<BidInfoJingjia> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return bidInfoJingjiaDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<BidInfoJingjia> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(bidInfoJingjiaDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		BidInfoJingjia bidInfoJingjia = vo.getObj();
		param.setResultCache(false);
		if (!Constant.getInstance().ifWriteDatabase()) {
			param.setResultCache(false);
		}
		if (Validator.isEmpty(bidInfoJingjia))
			return param;
		if (Validator.isNotNull(bidInfoJingjia.getItemId())) {
			sb.append(" and itemId = :itemId");
			param.setParams("itemId", bidInfoJingjia.getItemId());
		}
		if (Validator.isNotNull(bidInfoJingjia.getBidValue())) {
			sb.append(" and bidValue = :bidValue");
			param.setParams("bidValue", bidInfoJingjia.getBidValue());
		}
		if (Validator.isNotNull(bidInfoJingjia.getBidTime())) {
			sb.append(" and bidTime = :bidTime");
			param.setParams("bidTime", bidInfoJingjia.getBidTime());
		}
		if (Validator.isNotNull(bidInfoJingjia.getBidNum())) {
			sb.append(" and bidNum = :bidNum");
			param.setParams("bidNum", bidInfoJingjia.getBidNum());
		}
		if (Validator.isNotNull(bidInfoJingjia.getAmount())) {
			sb.append(" and amount = :amount");
			param.setParams("amount", bidInfoJingjia.getAmount());
		}
		if (Validator.isNotNull(bidInfoJingjia.getUserId())) {
			sb.append(" and userId = :userId");
			param.setParams("userId", bidInfoJingjia.getUserId());
		}
		if (Validator.isNotNull(bidInfoJingjia.getUserName())) {
			sb.append(" and userName = :userName");
			param.setParams("userName", bidInfoJingjia.getUserName());
		}
		if (Validator.isNotNull(bidInfoJingjia.getRightLevel())) {
			sb.append(" and rightLevel = :rightLevel");
			param.setParams("rightLevel", bidInfoJingjia.getRightLevel());
		}
		if (Validator.isNotNull(bidInfoJingjia.getUserIp())) {
			sb.append(" and userIp = :userIp");
			param.setParams("userIp", bidInfoJingjia.getUserIp());
		}
		if (Validator.isNotNull(bidInfoJingjia.getIncValue())) {
			sb.append(" and incValue = :incValue");
			param.setParams("incValue", bidInfoJingjia.getIncValue());
		}
		if (Validator.isNotNull(bidInfoJingjia.getTurn())) {
			sb.append(" and turn = :turn");
			param.setParams("turn", bidInfoJingjia.getTurn());
		}
		if (Validator.isNotNull(bidInfoJingjia.getBidType())) {
			sb.append(" and bidType = :bidType");
			param.setParams("bidType", bidInfoJingjia.getBidType());
		}
		if (Validator.isNotNull(bidInfoJingjia.getWeight())) {
			sb.append(" and weight = :weight");
			param.setParams("weight", bidInfoJingjia.getWeight());
		}
		if (Validator.isNotNull(bidInfoJingjia.getBidUserNo())) {
			sb.append(" and bidUserNo = :bidUserNo");
			param.setParams("bidUserNo", bidInfoJingjia.getBidUserNo());
		}
		param.setSqlStr(sb.toString());
		return param;
	}
}
